﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Text;

namespace DataContext
{
	public class NewsContext
	{
		#region Fields
		Database.Database db = new Database.Database(Database.Providers.SqlClient, Config.Global.Settings.ConnectionString);
		#endregion

		#region Constructors
		public NewsContext() {}
		#endregion

        #region Context Methods
        /// <summary>
        /// Search News
        /// </summary>
        /// <param name="text"></param>
        /// <param name="newsID"></param>
        /// <param name="currentPage"></param>
        /// <param name="pageSize"></param>
        /// <param name="totalRecord"></param>
        /// <returns></returns>
        public DataTable SearchNews(string text, int newsID, int currentPage, int pageSize, ref int totalRecord)
        {
            try
            {
                List<SqlParameter> lstParam = new List<SqlParameter>();

                lstParam.Add(new SqlParameter("@text", text));
                lstParam.Add(new SqlParameter("@NewsID", newsID));
                lstParam.Add(new SqlParameter("@CurrentPage", currentPage));
                lstParam.Add(new SqlParameter("@PageSize", pageSize));
                SqlParameter para = new SqlParameter("@TotalRecord", SqlDbType.Int);
                para.Direction = ParameterDirection.Output;
                lstParam.Add(para);

                DataTable dt = (DataTable)db.GetDataTable("[uspNewsSearch]", lstParam.ToArray());
                totalRecord = Convert.ToInt32(lstParam[lstParam.Count - 1].Value);

                return dt;
            }
            catch (System.Exception)
            {
                return new DataTable();
            }
        }

        /// <summary>
        /// Update News Status
        /// </summary>
        /// <param name="newsID"></param>
        /// <returns></returns>
        public bool UpdateStatus(int newsID)
        {
            try
            {
                List<SqlParameter> lstParam = new List<SqlParameter>();
                lstParam.Add(new SqlParameter("@NewsID", newsID));

                return (int)db.ExecuteStoreProcedure("[uspNewsUpdateStatus]", lstParam.ToArray()) > 0;
            }
            catch (System.Exception)
            {
                return false;
            }
        }

        /// <summary>
        /// Lấy NewsID mới nhất
        /// </summary>
        /// <returns></returns>
        public int SelectNewNewsID(int num)
        {
            try
            {
                List<SqlParameter> lstParam = new List<SqlParameter>();
                lstParam.Add(new SqlParameter("@Num", num));

                return (int)db.ExecuteStoreProcedure("[uspNewsSelectNewNewsID]", lstParam.ToArray());
            }
            catch (System.Exception)
            {
                return -1;
            }
        }

        /// <summary>
        /// Lấy các bản tin khác
        /// </summary>
        /// <param name="newsID"></param>
        /// <param name="num"></param>
        /// <returns></returns>
        public DataTable SelectOtherNews(int newsID, int num)
        {
            try
            {
                List<SqlParameter> lstParam = new List<SqlParameter>();
                lstParam.Add(new SqlParameter("@NewsID", newsID));
                lstParam.Add(new SqlParameter("@Num", num));

                return db.GetDataTable("uspNewsSelectOtherNews", lstParam.ToArray());
            }
            catch (System.Exception)
            {
                return null;
            }
        }
		#endregion
	}
}
